package com.yunhe.inspect.controller;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.inspect.domain.CompletionOfInspection;
import com.yunhe.inspect.domain.OrgInspectAnalysis;
import com.yunhe.inspect.domain.OrgPeopleInspectAnalysis;
import com.yunhe.inspect.domain.ProportionOfFormsByDepartment;
import com.yunhe.inspect.model.DataInspectSituationVo;
import com.yunhe.inspect.service.InspectAnalysisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhengbo
 * @date 2021/11/10 16:19
 * 巡检分析
 */

@Api(value = "InspectAnalysis", description = "巡检分析", tags = {"巡检分析"})
@RestController
@RequestMapping(value = "/inspectAnalysis")
public class InspectAnalysisController {

    @Autowired
    private InspectAnalysisService inspectAnalysisService;

    @ApiOperation("查询各部门人员巡检数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "组织id", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "timeType", value = "时间类型(day,week,month,quarterly,year)", dataType = "String", paramType = "query")
    })
    @GetMapping(value = "/getOrgCheckerData")
    public ResponseEntity<ResultObject<List<OrgPeopleInspectAnalysis>>> getOrgCheckerData(@RequestParam("orgId") Long orgId,
                                                                                          @RequestParam("timeType") String timeType) {
        List<OrgPeopleInspectAnalysis> orgPeopleInspectAnalysisList = inspectAnalysisService.countNumberOfInspectionOrders(orgId, timeType);
        return new ResponseEntity<>(new ResultObject<>(orgPeopleInspectAnalysisList), HttpStatus.OK);
    }

    @ApiOperation("巡检情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始日期", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "endTime", value = "结束日期", dataType = "String", paramType = "query", required = false)
    })
    @GetMapping(value = "/getInspectionSituation")
    public ResponseEntity<ResultObject<DataInspectSituationVo>> getInspectionSituation(@RequestParam(value = "startTime",required = false) String startTime,
                                                                                       @RequestParam(value = "endTime",required = false) String endTime) throws ArgumentErrorException {

        List<ProportionOfFormsByDepartment> proportionOfFormsByDepartmentList =
                inspectAnalysisService.countNumberOfInspectionOrdersForEachDepartment(startTime, endTime);
        CompletionOfInspection completionOfInspection = inspectAnalysisService.percentageOfCompletedInspectionOrders(startTime, endTime);

        DataInspectSituationVo vo = new DataInspectSituationVo();
        vo.setCompletionOfInspection(completionOfInspection);
        vo.setProportionOfFormsByDepartmentList(proportionOfFormsByDepartmentList);

        return new ResponseEntity<>(new ResultObject<>(vo), HttpStatus.OK);
    }

    @ApiOperation("巡检部门")
    @ApiImplicitParams({})
    @GetMapping(value = "/getInspectionOrgList")
    public ResponseEntity<ResultObject<List<OrgInspectAnalysis>>> getInspectionOrgList() throws ArgumentErrorException {
        List<OrgInspectAnalysis> orgInspectAnalysisList = inspectAnalysisService.getOrgInspectAnalysisList();
        return new ResponseEntity<>(new ResultObject<>(orgInspectAnalysisList), HttpStatus.OK);
    }
}
